﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;

namespace XcoAppSpaces.Contracts.Transport
{
	/// <summary>
	/// Interface definition for a message to be sent by remote communication.
	/// </summary>
	public class XcoMessage
	{
		/// <summary>
		/// Event that is raised by the transport service when the transfer of the message was successful.
		/// </summary>
		public event Action<XcoMessage> OnTransferSuccessful;

		/// <summary>
		/// Event that is raised by the transport service when the transfer of the message failed.
		/// </summary>
		public event Action<XcoMessage, Exception> OnTransferFailed;

		/// <summary>
		/// The causality context of the message, defining the causalities
		/// that should be installed for processing the message.
		/// </summary>
		public byte[] CausalityContext { get; set; }

		/// <summary>
		/// The content of the message.
		/// </summary>
		public byte[] Content { get; set; }

		/// <summary>
		/// Info about the message content that can be used for an error message if deserialization fails.
		/// </summary>
		public string ContentInfo { get; set; }


		/// <summary>
		/// Method to allow the transport service raising the OnTransferSuccessful event.
		/// </summary>
		public void RaiseTransferSuccessfulEvent()
		{
			if (OnTransferSuccessful != null)
				OnTransferSuccessful(this);
		}

		/// <summary>
		/// Method to allow the transport service raising the OnTransferFailed event.
		/// </summary>
		/// <param name="ex">Exception explaining why the transfer failed.</param>
		public void RaiseTransferFailedEvent(Exception ex)
		{
			if (OnTransferFailed != null)
				OnTransferFailed(this, ex);
		}
	}
}
